package com.example.myapplication.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.myapplication.R;
import com.example.myapplication.base.BaseLazyFragment;
import com.example.myapplication.bean.SquaseBean;
import com.example.myapplication.presenter.SquarePresenter;
import com.example.myapplication.ui.activity.WebViewActivity;
import com.example.myapplication.ui.adapter.SquareAdapter;
import com.example.myapplication.view.SquareView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.List;

//https://wanandroid.com/user_article/list/0/json

/**
 * 这是广场
 */
public class SquareFragment extends BaseLazyFragment<SquarePresenter> implements SquareView {

    private Toolbar toolbar;
    private RecyclerView rv;
    private SquareAdapter squareAdapter;
    private int page=0;
    private SmartRefreshLayout srl;
    private List<SquaseBean.DataBean.DatasBean> datas;

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {
        mPresenter.getSquareData(page);
    }

    @Override
    protected void initView(View view) {
        toolbar = (Toolbar) view.findViewById(R.id.toolbar);
        rv = (RecyclerView) view.findViewById(R.id.rv);
        srl = view.findViewById(R.id.srl);
        rv.setLayoutManager(new LinearLayoutManager(getContext()));
        squareAdapter = new SquareAdapter(getActivity());
        rv.setAdapter(squareAdapter);
        srl.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                initData();
                Log.d(TAG, "onLoadMore: ");

            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page=0;
                if (datas!=null&&datas.size()>0){
                    datas.clear();
                    initData();
                }

            }
        });
        squareAdapter.setOnItemClickListener(new SquareAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(int position, List<SquaseBean.DataBean.DatasBean> list) {
                Intent intent = new Intent(getActivity(), WebViewActivity.class);
                intent.putExtra("like",list.get(position).getLink());
                startActivity(intent);
            }
        });
    }

    @Override
    protected SquarePresenter initPresenter() {
        return new SquarePresenter();
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_square;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }


    @Override
    public void setData(SquaseBean squaseBean) {
        Log.d(TAG, "setData: " + squaseBean);
        datas = squaseBean.getData().getDatas();
        squareAdapter.setList(datas);
        srl.finishLoadMore();
        srl.finishRefresh();
    }

    private static final String TAG = "SquareFragment";
}